<?php

namespace App\Http\Controllers\Steam;

use App\DB\Steam\SteamGameDB;
use App\Http\Controllers\Controller;
use App\Model\Steam\SteamGame;
use Illuminate\Http\Request;

class SteamGameController extends Controller
{
    protected $picturePath = 'upload/image/steam/game/';
    protected $thumbPath = 'upload/image/steam/game/thumb/';
//    protected $filePath = env('FILE_URL');

    /**
     * 精品推荐
     *
     * @param Request $request
     * @return array
     */
    public function recommend(Request $request)
    {
        $page = $request->has('page') ? $request->input('page') : 1; # 当前页数
        $limit = $request->has('limit') ? $request->input('limit') : 10; # 每页显示条数
        $result = SteamGame::recommendGame($page, $limit);
        $respons = [];
        $respons['data'] = $result['data'];
        $respons['picture_path'] = env('FILE_URL') . $this->picturePath;
        $respons['thumb_path'] = env('FILE_URL') . $this->thumbPath;
        $respons['count'] = $result['count'];
        $respons['page_count'] = $result['page_count'];
        return $this->success($respons);
    }

    /**
     * 特别优惠
     * @param Request $request
     * @return array
     */
    public function preferential(Request $request)
    {
        $page = $request->has('page') ? $request->input('page') : 1; # 当前页数
        $limit = $request->has('limit') ? $request->input('limit') : 10; # 每页显示条数
        $result = SteamGame::preferentialGame($page, $limit);
        $respons = [];
        $respons['data'] = $result['data'];
        $respons['picture_path'] = env('FILE_URL') . $this->picturePath;
        $respons['thumb_path'] = env('FILE_URL') . $this->thumbPath;
        $respons['count'] = $result['count'];
        $respons['page_count'] = $result['page_count'];
        return $this->success($respons);
    }

    /**
     * 游戏列表查看
     * @param Request $request
     * @return array
     */
    public function browse(Request $request)
    {
        $page = $request->has('page') ? $request->input('page') : 1; # 当前页数
        $limit = $request->has('limit') ? $request->input('limit') : 10; # 每页显示条数
        $tab = $request->has('tab') ? $request->input('tab') : 'new'; # 选项卡选择的查询项
        $category = $request->has('category') ? $request->input('category') : 0; # 分类ID
        $tag = $request->has('tag') ? $request->input('tag') : 0; # 分类ID
        $result = SteamGameDB::browseGame($tab, $category, $tag, $page, $limit);
        if(!$result) {
            return $this->error(1, 'No found data!');
        }
        $result['picture_path'] = env('FILE_URL') . $this->picturePath;
        $result['thumb_path'] = env('FILE_URL') . $this->thumbPath;
        return $this->success($result);
    }

    /**
     * 游戏详情列表
     * @param Request $request
     * @return array
     */
    public function detailList(Request $request)
    {
        $page = $request->has('page') ? $request->input('page') : 1; # 当前页数
        $limit = $request->has('limit') ? $request->input('limit') : 10; # 每页显示条数
        $tab = $request->has('tab') ? $request->input('tab') : ['new', 'hot', 'soon', 'discount']; # 选项卡数组，即要查询的内容
        $result = [];
        foreach ($tab as $key => $val) {
            $result[$val] = SteamGame::detailList($val, $page, $limit);
        }
//        echo '<meta charset="utf-8"><pre>'; print_r($result);die('</pre>');
        $result['picture_path'] = env('FILE_URL') . $this->picturePath;
        $result['thumb_path'] = env('FILE_URL') . $this->thumbPath;
        return $this->success($result);
    }


    /**
     * 游戏详情信息
     * @param Request $request
     * @return array
     */
    public function details(Request $request)
    {
        if(!$request->has('id')) {
            return $this->error(1, 'No found id!');
        }
        $id = $request->input('id');
        $result = SteamGameDB::getGameDetails($id);
        if(!$result) {
            return $this->error(1, 'No found data!');
        }
        $result['picture_path'] = env('FILE_URL') . $this->picturePath;
        $result['thumb_path'] = env('FILE_URL') . $this->thumbPath;
        return $this->success($result);
    }

}
